package com.danger.Activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.danger.Util.ImageHelper;

public class PrimaryColorActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    private ImageView imageView;
    private SeekBar hue_seekBar;
    private SeekBar saturation_seekBar;
    private SeekBar lum_seekBar;
    private static final int MAX_VALUE = 255;
    private static final int MID_VALUE = 127;
    //设置变量保存RGB
    private float hue;
    private float saturation;
    private float lum;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_primary_color);

        initView();
        initEvent();

    }

    private void initView() {
        hue = (MID_VALUE - MID_VALUE) * 1.0f / MID_VALUE * 180;
        saturation = MID_VALUE * 1.0f / MID_VALUE;
        lum = MID_VALUE * 1.0f / MID_VALUE;
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.test1);
        imageView = (ImageView) findViewById(R.id.imageView);
        hue_seekBar = (SeekBar) findViewById(R.id.hue_seekBar);
        saturation_seekBar = (SeekBar) findViewById(R.id.saturation_seekBar);
        lum_seekBar = (SeekBar) findViewById(R.id.lum_seekBar);

        imageView.setImageBitmap(bitmap);

        //设置SeekBar的最大值
        hue_seekBar.setMax(MAX_VALUE);
        saturation_seekBar.setMax(MAX_VALUE);
        lum_seekBar.setMax(MAX_VALUE);
        //设置SeekBar刻度最初的位置
        hue_seekBar.setProgress(MID_VALUE);
        saturation_seekBar.setProgress(MID_VALUE);
        lum_seekBar.setProgress(MID_VALUE);
    }

    private void initEvent() {
        hue_seekBar.setOnSeekBarChangeListener(this);
        saturation_seekBar.setOnSeekBarChangeListener(this);
        lum_seekBar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.hue_seekBar:
                hue = (progress - MID_VALUE) * 1.0f / MID_VALUE * 180;
                break;
            case R.id.saturation_seekBar:
                saturation = progress * 1.0f / MID_VALUE;
                break;
            case R.id.lum_seekBar:
                lum = progress * 1.0f / MID_VALUE;
                break;
            default:
                break;
        }
        imageView.setImageBitmap(ImageHelper.handleImageEffect(bitmap, hue, saturation, lum));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}
